﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Mimoza.GUI;
using Mimoza.ObjectLayer;
using Mimoza.Apps.Console.ViewModel;
using Mimoza.Apps.Console.Commands;
using Mimoza.Apps.Console.Resources;

namespace Mimoza.Apps.Console
{
    public partial class PolicyWizardGeneralPage : Mimoza.GUI.WizardPage
    {
        Policy policy = null;

        public PolicyWizardGeneralPage()
        {
            InitializeComponent();
        }
        override public void Init()
        {
            policy = (this.Wizard as PolicyWizard).Policy;
            if (null != this.policy)
            {
                this.policyName.Text = this.policy.Name;
                this.policyDescription.Text = this.policy.Description;
                this.policyOverride.Checked = this.policy.Override;
                this.policyDisabled.Checked = !this.policy.IsEnabled;
            }
            this.IsConfigured = PageIsConfigured();

            this.policyName.TextChanged += new System.EventHandler(this.policyName_TextChanged);
            this.policyDisabled.CheckedChanged += new System.EventHandler(this.policyDisabled_CheckedChanged);
            this.policyDescription.TextChanged += new System.EventHandler(this.policyDescription_TextChanged);
            this.policyOverride.CheckedChanged += new System.EventHandler(this.policyOverride_CheckedChanged);
        }
        override public bool Try2LeavePage(WizardPageLeavingDirection direction)
        {
            if (direction == WizardPageLeavingDirection.Back)
            {
                return true;
            }

            if (this.policyName.Text != null && this.policyName.Text.Length != 0)
            {
                if (null != this.policy)
                {
                    this.policy.Name = this.policyName.Text;
                    this.policy.Description = this.policyDescription.Text;
                    this.policy.Override = this.policyOverride.Checked;
                    this.policy.IsEnabled = !this.policyDisabled.Checked;
                }
                //this.IsConfigured = true;
                return true;
            }

            MessageBox.Show(Resource.PolicyWizardGeneralPage_ErrorText, Resource.PolicyWizardGeneralPage_Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
            return false;
        }

        private bool PageIsConfigured()
        {
            bool res = true;
            if (this.policyName.Text == null || this.policyName.Text.Length <= 0) res = false;
            //if (this.policyDescription.Text == null || this.policyDescription.Text.Length <= 0) res = false;
            return res;
        }

        private void policyName_TextChanged(object sender, EventArgs e)
        {
            this.IsConfigured = PageIsConfigured();
            this.RaiseOnPageModified();
        }

        private void policyDescription_TextChanged(object sender, EventArgs e)
        {
            this.IsConfigured = PageIsConfigured();
            this.RaiseOnPageModified();
        }

        private void policyDisabled_CheckedChanged(object sender, EventArgs e)
        {
            this.IsConfigured = PageIsConfigured();
            this.RaiseOnPageModified();
        }

        private void policyOverride_CheckedChanged(object sender, EventArgs e)
        {
            this.IsConfigured = PageIsConfigured();
            this.RaiseOnPageModified();
        }
    }
}
